#include "NetIO.h"

Operation_Queue::Operation_Queue( )
	:operations_( 0 )
{
	operations_ = new Operations;
	TAILQ_INIT( operations_ );
}

Operation_Queue::~Operation_Queue( )
{
	Operation *op = 0;
	while( !TAILQ_EMPTY( operations_ ) )
	{
		op = TAILQ_FIRST( operations_ );
		TAILQ_REMOVE( operations_, op, field_ );
		op->destroy( );
	}
}

Operation *Operation_Queue::front( )
{
	return TAILQ_FIRST( operations_ );
}

void Operation_Queue::pop( )
{
	if( TAILQ_EMPTY( operations_ ) )
		return;
	TAILQ_REMOVE( operations_, TAILQ_FIRST( operations_ ), field_ );
}

void Operation_Queue::push( Operation *op )
{
	TAILQ_INSERT_TAIL( operations_, op, field_ );
}

bool Operation_Queue::empty( )
{
	return TAILQ_EMPTY( operations_ );
}

void Operation_Queue::concat( Operation_Queue *op_queue )
{
	TAILQ_CONCAT( operations_, op_queue->operations_, field_ );
}

void Socket::close( )
{
	if( handle_ != INVALID_HANDLE )
		SockOps::close_socket( handle_ );

	if( handle_data_ )
		handle_data_->destroy( );

	handle_ = INVALID_HANDLE;
	handle_data_ = 0;
}
